var formData=new FormData();

var xingzuo="";

var sheng="";
var shi="";
var qu="";
window.onload=function(){
    // 获取用户资料
    if(localStorage.token !=undefined){
        document.querySelector(".user").style.display="inline-block";
        document.querySelector(".not_login").style.display="none";
        document.getElementsByClassName('userName')[0].textContent = localStorage.name;
        var avatar= document.getElementsByClassName('userHead');
        document.getElementsByClassName('userHead')[0].src = url_file+localStorage.avatar;
        document.querySelector('.logout').onclick=function(){
            localStorage.removeItem("token");
        }
        // var personan_a=document.getElementById('personal-main');
        // personan_a.href=window.location.href+'?'+localStorage.avatar+'?'+localStorage.name+'?'+'?'+localStorage.constellations+'?'+localStorage.gender+'?'+localStorage.province;
        // 获取用户修改后的头像
        // 图片填充(接口返回的图片地址需要前加上服务器地址，如果图片带有http则不需要拼接。)
        // 判断图片地址是否含有http
        // if (avatar.indexOf('http') === -1) {
        //     // indexOf用于判断字符串是否还有指定字段，如果没有则返回-1
        //     document.getElementsByClassName('userHead')[0].src = 'http://localhost:8080/'+ avatar;
        //  } else {
        //     document.getElementsByClassName('userHead')[0].src = avatar;
        //  }        
    }else{
        document.querySelector(".not_login").style.display="inline-block";
        document.querySelector(".user").style.display="none";
    };    
    saveMsg();    
}
    
    // 获取用户选中的星座
    document.getElementById('constellation').onchange=function(){
        console.log(constellation.value);
        xingzuo=document.getElementById('constellation').value;
    }

    //市级联动菜单
    document.querySelector('#selectProvince').onchange=function(){
        var selectCity=document.getElementById('selectCity');
        //每次省级菜单onchange时，都清空市级菜单里面的option，再加入onchange之后的市级菜单
        selectCity.options.length=0;
        selectCity.style.display="inline-block";
        //document.getElementById("selectProvince").selectedIndex 获取select的下标
        //document.getElementById("selectProvince").options  获取select下options的value 获取到的值是字符串
        //将字符串转为整数类型赋给abc
        var abc=parseInt(document.getElementById("selectProvince").options[document.getElementById("selectProvince").selectedIndex].value);
        //abc为选中省份的下标  abc=ProID
        console.log(abc);
        // 获取市级
        ajaxXHR('GET',url+'city/city?ProID='+abc,function(data){
            //给包裹select的div 追加一个select        
            var city_city=data.data.city;
            console.log(city_city);
            selectCity.add(new Option("——请选择——",0));
            for(var i=0;i<city_city.length;i++){
                //给每个option的文本添加province[i].name,给value添加成ID值
                document.getElementById('selectCity').options.add(new Option(city_city[i].name,city_city[i].CityID));
                // console.log(selectProvince.options);
            }
            // if(data.code=="param_incomplete"){
            //     alert("请选择您所在的城市");
            // }else{
            //     return;
            // }
        })
        sheng =parseInt(document.getElementById("selectProvince").options[document.getElementById("selectProvince").selectedIndex].value);
        sheng1=document.getElementById("selectProvince").options[document.getElementById("selectProvince").selectedIndex].innerHTML;
    }
    //区级联动菜单
    document.getElementById("selectCity").onchange=function(){
        var selectArea=document.getElementById('selectarea');
        //每次省级菜单onchange时，都清空市级菜单里面的option，再加入onchange之后的市级菜单
        selectArea.options.length=0;
        selectArea.style.display="inline-block";
        //document.getElementById("selectCity").selectedIndex 获取select的下标
        //document.getElementById("selectCity").options  获取select下options的value 获取到的值是字符串
        //将字符串转为整数类型赋给abc
        var abc=parseInt(document.getElementById("selectCity").options[document.getElementById("selectCity").selectedIndex].value);
        //abc为选中市级ID CityID
        console.log(abc);
        // 获取市级
        ajaxXHR('GET',url+'city/area?CityID='+abc,function(data){
            //给包裹select的div 追加一个select        
            var area_area=data.data.area;
            console.log(area_area);
            selectArea.add(new Option("——请选择——",0));
            for(var i=0;i<area_area.length;i++){            
                //给每个option的文本添加province[i].name,给value添加成ID值
                selectArea.options.add(new Option(area_area[i].DisName,area_area[i].Id));
                // console.log(selectProvince.options);
            }
            // if(data.code=="param_incomplete"){
            //     alert("请选择您所在的城市");
            // }else{
            //     return;
            // }
        })
        shi =parseInt(document.getElementById("selectCity").options[document.getElementById("selectCity").selectedIndex].value);
        shi1=document.getElementById("selectCity").options[document.getElementById("selectCity").selectedIndex].innerHTML;
    }

    //获取选中的区
    document.getElementById('selectarea').onchange=function(){
        var abc=parseInt(document.getElementById("selectarea").options[document.getElementById("selectarea").selectedIndex].value);
        
        console.log(abc);
        qu=parseInt(document.getElementById("selectarea").options[document.getElementById("selectarea").selectedIndex].value);
        qu1=document.getElementById("selectarea").options[document.getElementById("selectarea").selectedIndex].innerHTML;
    }

//上传用户资料
document.getElementById('SettingBtn').onclick=function(){
    //判断密码输入是否正确
    var upwd=document.querySelector('.userPwd').value;
    if(upwd==""){
        alert("请输入密码");
    }else if( upwd == localStorage.pwd){
        // var Readder=dataURItoBlob(Readder1);
        var choice="";
        // 获取用户选择的性别   *****需要在点击提交时候来判断用户选择
        var getSex = document.querySelectorAll('#create_user_sex_rig input');
        for(var j=0;j<getSex.length;j++){
            if(getSex[j].checked){
                choice=getSex[j].value;
                console.log("选择的性别为："+choice);
            }
        } 
        var formData=new FormData();
        var ssq=["["+sheng,shi,qu+"]"];//获取所选中的省市区
        var ssq2=sheng1+shi1;
        var uName=document.getElementById('uName').value;
        var token=localStorage.token;
        userName=uName;

        formData.append("token",token);
        formData.append("avatar",Readder1); 
        formData.append("gender",choice);
        formData.append("city",ssq);
        formData.append("constellation",xingzuo);
        formData.append("name",uName);
        postXHR('POST',url+'account/profile',function(data){
            // var getSex = document.getElementById('create_user_sex_rig');      
            console.log(data);
            //保存用户信息
            localStorage.token=data.data.user.token;
            localStorage.avatar=data.data.user.avatar;
            localStorage.gender=choice;
            localStorage.name=data.data.user.name;
            localStorage.constellation=xingzuo;
            localStorage.city=ssq;
            localStorage.province=ssq2;
            localStorage.savessq=data.data.user.city;
            window.location.href="ArticleList.html";            
        },formData);
    }else{
        alert("密码错误");
        return false;
    }
}

function saveMsg(){
    if(localStorage.token!=undefined){
        // 头像
        document.getElementById("preview").setAttribute("src",url_file+localStorage.avatar);
        // 昵称
        document.getElementById("uName").value=localStorage.name;
        // 性别
        if(localStorage.gender=="man"){
            document.querySelectorAll("#create_user_sex_rig input")[0].checked=true;
            document.querySelectorAll("#create_user_sex_rig label")[0].classList.add("checked");
        }else{
            document.querySelectorAll("#create_user_sex_rig input")[1].checked=true;
            document.querySelectorAll("#create_user_sex_rig label")[1].classList.add("checked");
        }
        // 星座
        if(localStorage.constellation != "undefined"){
            ajaxXHR('GET',url+"constellations/query",function(data){
                if(data.code!="SUCCESS"){
                    document.querySelector("#constellation_star #constellation").insertAdjacentHTML('afterbegin','<option>无法加载信息</option>');
                    return;
                }
                var str='<option>'+localStorage.constellation+'</option>';
                for(i=0;i<data.data.constellations.length;i++){
                    if(data.data.constellations[i]==localStorage.constellation){
                        console.log("选中星座:"+data.data.constellations[i]);
                        data.data.constellations.splice(i,1);
                        
                        for(i=0;i<data.data.constellations.length;i++){
                            str+='<option value="'+data.data.constellations[i]+'">'+data.data.constellations[i]+'</option>';
                        }
                    }
                }
                document.querySelector("#constellation_star #constellation").insertAdjacentHTML('afterbegin',str);
                console.log(data);
            })
        }


        // 城市
        var city=localStorage.savessq;
        city=city.split(",");
        var provinceId=city[0];
        // console.log("省的id"+province);
        var CityID=city[1];
        // console.log("市的id"+city);
        var Id=city[2];
        // console.log("县的id"+county);

        var city_province = "";
        var city_city ="";
        ajaxXHR('GET',url+'city/province?ProID='+provinceId,function(data){
            // console.log(provinceId);
            // console.log(data);
            city_province=data.data.province;
            console.log("省："+city_province);
        })
        ajaxXHR('GET',url+'city/province',function(data){
            if(data.code !=="SUCCESS"){
                document.getElementById("selectProvince").insertAdjacentHTML('afterbegin','<option>无法加载信息</option>');
                return;
            }
            var data_province=data.data.province;
            // console.log("选中的省："+data_province);
            var str='<option value="'+provinceId+'">'+city_province+'</option>';
            for(i=0;i<data_province.length;i++){
                if(data_province[i].name==city_province){
                    console.log("data_province[i].name"+data_province[i].name);
                    data_province.splice(i,1);
                    for(i=0;i<data_province.length;i++){
                        str+='<option value="'+data_province[i].provinceId+'">'+data_province[i].name+'</option>';
                    }
                }
            }
            console.log("str"+str);
            document.getElementById("selectProvince").insertAdjacentHTML('afterbegin',str);
            console.log("data"+data);
        })
    }else if(localStorage.token==undefined){
        // 星座
        console.log(1)
        ajaxXHR('GET',url+'constellations/query',function(data){
            var constellation=document.getElementById('constellation');
            var star=data.data.constellations;
            // console.log(star);
            document.querySelector("#constellation_star #constellation").insertAdjacentHTML('afterbegin','<option>--请选择--</option>');
            for(var i=0;i<star.length;i++){
                constellation.options.add(new Option(star[i],star[i]));
                // var abc=parseInt(document.getElementById("selectProvince").options[document.getElementById("selectProvince").selectedIndex].value);
            }
        })

        // 获取省份
        ajaxXHR('GET',url+'city/province',function(data){        
            var province=data.data.province;
            // console.log(province);
            document.querySelector("#connection #selectProvince").insertAdjacentHTML('afterbegin','<option>--请选择--</option>');
            for(var i=0;i<province.length;i++){            
                //给每个option的文本添加province[i].name,给value添加成ID值
                document.getElementById('selectProvince').options.add(new Option(province[i].name,province[i].ProID));
                // console.log(selectProvince.options);           
                
            }
        });
    }
}